package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_alias_mixin;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_alias_mixin;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_alias_mixinSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_alias_mixinService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[EMail别名 Mixin] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_alias_mixinServiceImpl implements IMail_alias_mixinService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_alias_mixinClientService mail_alias_mixinClientService;

    @Override
    public void createBatch(List<Mail_alias_mixin> mail_alias_mixins, int batchSize) {

                        //mail_alias_mixinClientService.createBatch(mail_alias_mixins,batchSize) ;

    }

    @Override
    public boolean create(Mail_alias_mixin mail_alias_mixin) {
        boolean bOk = false ;
        Imail_alias_mixin clientModel = this.convert2Model(mail_alias_mixin,null);
        mail_alias_mixinClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_alias_mixin);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_alias_mixin clientModel = mail_alias_mixinClientService.createModel();
        clientModel.setId(id);
        mail_alias_mixinClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Mail_alias_mixin> mail_alias_mixins, int batchSize) {

        //mail_alias_mixinClientService.updateBatch(mail_alias_mixins,batchSize) ;

    }

    @Override
    public boolean update(Mail_alias_mixin mail_alias_mixin) {
        boolean bOk = false ;
        Imail_alias_mixin clientModel = this.convert2Model(mail_alias_mixin,null);
        mail_alias_mixinClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_alias_mixin) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Mail_alias_mixin get(Integer id) {
        Imail_alias_mixin clientModel = mail_alias_mixinClientService.createModel();
        clientModel.setId(id);
        mail_alias_mixinClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_alias_mixin get(Mail_alias_mixin mail_alias_mixin) {
        Mail_alias_mixin retmail_alias_mixin = this.get(mail_alias_mixin.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_alias_mixin.getClass(), mail_alias_mixin.getClass(), false);
        copier.copy(retmail_alias_mixin, mail_alias_mixin, null);
        return mail_alias_mixin ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_alias_mixinClientService.removeBatch(mail_alias_mixins) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_alias_mixin> searchDefault(Mail_alias_mixinSearchContext context) {
        List<Mail_alias_mixin> list = new ArrayList<Mail_alias_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_alias_mixin> clientModelList = mail_alias_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_alias_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_alias_mixin>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail别名查询
     */
    @Override
    public Page<Mail_alias_mixin> selectByOdoo_alias(Integer id){
        Mail_alias_mixinSearchContext context = new Mail_alias_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_alias_mixin> list = new ArrayList<Mail_alias_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_alias_mixin> clientModelList = mail_alias_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_alias_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_alias_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_alias_mixin convert2Model(Mail_alias_mixin domain , Imail_alias_mixin model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_alias_mixinClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getAlias_parent_thread_idDirtyFlag())
            model.setAlias_parent_thread_id(domain.getAlias_parent_thread_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getAlias_domainDirtyFlag())
            model.setAlias_domain(domain.getAlias_domain());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getAlias_defaultsDirtyFlag())
            model.setAlias_defaults(domain.getAlias_defaults());
         if(domain.getAlias_nameDirtyFlag())
            model.setAlias_name(domain.getAlias_name());
         if(domain.getAlias_contactDirtyFlag())
            model.setAlias_contact(domain.getAlias_contact());
         if(domain.getAlias_model_idDirtyFlag())
            model.setAlias_model_id(domain.getAlias_model_id());
         if(domain.getAlias_force_thread_idDirtyFlag())
            model.setAlias_force_thread_id(domain.getAlias_force_thread_id());
         if(domain.getAlias_parent_model_idDirtyFlag())
            model.setAlias_parent_model_id(domain.getAlias_parent_model_id());
         if(domain.getAlias_user_idDirtyFlag())
            model.setAlias_user_id(domain.getAlias_user_id());
         if(domain.getAlias_idDirtyFlag())
            model.setAlias_id(domain.getAlias_id());
        return model ;
    }

    public Mail_alias_mixin convert2Domain( Imail_alias_mixin model ,Mail_alias_mixin domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_alias_mixin();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getAlias_parent_thread_idDirtyFlag())
            domain.setAlias_parent_thread_id(model.getAlias_parent_thread_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getAlias_domainDirtyFlag())
            domain.setAlias_domain(model.getAlias_domain());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getAlias_defaultsDirtyFlag())
            domain.setAlias_defaults(model.getAlias_defaults());
        if(model.getAlias_nameDirtyFlag())
            domain.setAlias_name(model.getAlias_name());
        if(model.getAlias_contactDirtyFlag())
            domain.setAlias_contact(model.getAlias_contact());
        if(model.getAlias_model_idDirtyFlag())
            domain.setAlias_model_id(model.getAlias_model_id());
        if(model.getAlias_force_thread_idDirtyFlag())
            domain.setAlias_force_thread_id(model.getAlias_force_thread_id());
        if(model.getAlias_parent_model_idDirtyFlag())
            domain.setAlias_parent_model_id(model.getAlias_parent_model_id());
        if(model.getAlias_user_idDirtyFlag())
            domain.setAlias_user_id(model.getAlias_user_id());
        if(model.getAlias_idDirtyFlag())
            domain.setAlias_id(model.getAlias_id());
        return domain ;
    }


}
